package com.his.outpatientvisit.service.impl;

import com.his.config.annotation.CurrentUser;
import com.his.dictresource.dao.ResourceControlMapper;
import com.his.dictresource.entity.ResourceControl;
import com.his.login.entity.DictEmployee;
import com.his.outpatientvisit.dao.PatiOutVisitMapper;
import com.his.outpatientvisit.entity.PatiOutVisit;
import com.his.outpatientvisit.entity.PatiOutVisitStatementList;
import com.his.outpatientvisit.service.PatiOutVisitService;
import com.his.outpatientvisit.vo.PatiOutVisitVo;
import com.xiao.common.util.BeanUtil;
import com.xiao.common.util.WorkIdUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PatiOutVisitServiceImpl implements PatiOutVisitService {

    @Resource
    private PatiOutVisitMapper patiOutVisitMapper;

    @Resource
    private ResourceControlMapper resourceControlMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String insertPatiOutVisit(DictEmployee employee,PatiOutVisitVo patiOutVisitVo) throws Exception{
        String serialNumber= WorkIdUtil.nextId();
        patiOutVisitVo.setSerialnumber(serialNumber);
        //作废人员
        patiOutVisitVo.setCancempid("0");
        patiOutVisitVo.setHospid(employee.getHOSPID());
        patiOutVisitVo.setEmpid(employee.getEMPID());
        //设置门诊状态
        patiOutVisitVo.setOutstatusid("00");
        //设置号源有效期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String regDate = patiOutVisitVo.getRegdate();
        String start = regDate +" 00:00:00";
        String end = regDate +" 23:59:59";
        Date validDate1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(start);
        Date validDate2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(end);
        patiOutVisitVo.setValiddate1(validDate1);
        patiOutVisitVo.setValiddate2(validDate2);
        //就诊卡号
        patiOutVisitVo.setCardid("999999999999");
        //结算标识 0未 1已
        patiOutVisitVo.setSettleflag("0");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date reg = sdf.parse(patiOutVisitVo.getRegdate());

        PatiOutVisit patiOutVisit = BeanUtil.copyBean(patiOutVisitVo, PatiOutVisit.class);
        patiOutVisit.setRegdate(reg);


        patiOutVisitMapper.outvisit_add(patiOutVisit);
        return serialNumber;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePatiOutVisit(DictEmployee employee,PatiOutVisitVo patiOutVisitVo) {
        PatiOutVisit patiOutVisit = BeanUtil.copyBean(patiOutVisitVo,PatiOutVisit.class);
        patiOutVisit.setCancdate(new Date());
        patiOutVisit.setCancempid(employee.getEMPID());
        patiOutVisitMapper.outvisit_update(patiOutVisit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PatiOutVisit> selectPatiOutVisitList(PatiOutVisitVo patiOutVisitVo) throws Exception{
        PatiOutVisit patiOutVisit = BeanUtil.copyBean(patiOutVisitVo,PatiOutVisit.class);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date =simpleDateFormat.parse(patiOutVisitVo.getRegdate());
        patiOutVisit.setRegdate(date);
        return patiOutVisitMapper.outvisit_list(patiOutVisit);
    }

    @Override
    public List<PatiOutVisitStatementList> selectPatiOutVisitStatementList(DictEmployee employee){
        PatiOutVisit patiOutVisit = new PatiOutVisit();
        patiOutVisit.setRegdate(new Date());
        patiOutVisit.setHospid(employee.getHOSPID());
        return  patiOutVisitMapper.statementlist_select(patiOutVisit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer selectPatiOutVisitSerialNo(DictEmployee employee,PatiOutVisitVo patiOutVisitVo) throws Exception{
        PatiOutVisit patiOutVisit = BeanUtil.copyBean(patiOutVisitVo,PatiOutVisit.class);

//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        Date date =simpleDateFormat.parse(patiOutVisitVo.getRegdate());
        Date date = new Date();
        //查询号源最大数量
        ResourceControl resourceControl =new ResourceControl();
        resourceControl.setCurdate(date);
        resourceControl.setHospid(employee.getHOSPID());
        resourceControl.setOrgid(patiOutVisit.getDiagorgid());
        if(patiOutVisit.getDoctorempid() != null){
            resourceControl.setResourceid(patiOutVisit.getDoctorempid());
        }
        Integer limitamount = resourceControlMapper.limitamount(resourceControl).getLimitamout();

        //查询门诊当前号源数
        patiOutVisit.setRegdate(date);
        patiOutVisit.setHospid(employee.getHOSPID());
        int sernalNo = 0 ;
        if(patiOutVisitMapper.outvisit_sernalno(patiOutVisit) != null){
            sernalNo = patiOutVisitMapper.outvisit_sernalno(patiOutVisit);
        }
        if(sernalNo < limitamount){
            return sernalNo+1;
        }else{
            return -1;
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void receivepati(DictEmployee employee,PatiOutVisitVo patiOutVisitVo) throws Exception{
        PatiOutVisit patiOutVisit = BeanUtil.copyBean(patiOutVisitVo,PatiOutVisit.class);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date nowTime = simpleDateFormat.parse(simpleDateFormat.format(new Date()));
        patiOutVisit.setVisitdatestart(nowTime);//就诊时间
        patiOutVisit.setOutstatusid("02");//就诊状态
        patiOutVisit.setDoctorempid(employee.getEMPID());//接诊医生
        patiOutVisitMapper.outvisit_update(patiOutVisit);
    }

    /** 
     * 
     * 取消接诊
     * [employee, patiOutVisitVo] 
     * @return void
     * @date 2020/3/26 16:24
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void receivepati_canc(DictEmployee employee, PatiOutVisitVo patiOutVisitVo) {
        PatiOutVisit patiOutVisit = BeanUtil.copyBean(patiOutVisitVo,PatiOutVisit.class);

        PatiOutVisit pov = patiOutVisitMapper.outvisit(patiOutVisit);

        pov.setVisitdatestart(null);
        pov.setOutstatusid("00");
        if(pov.getRsrctypeid().equals("001000")){
            pov.setDoctorempid("0");
        }
        patiOutVisitMapper.outvisit_updates(pov);
    }
}
